<template>
  <div class="box">
    <el-form ref="userForm" :model="forms" :rules="userRules" :label-position="labelPosition" label-width="100px">
      <el-form-item prop="name" label="企业名称"><el-input size="medium" v-model="forms.name" placeholder="请输入企业名称"></el-input></el-form-item>
      <el-form-item prop="corp_id" label="企业id"><el-input size="medium" v-model="forms.corp_id" placeholder="请输入企业id"></el-input></el-form-item>
      <el-form-item>
        <el-button size="medium" @click="addSave" v-if="user_type == 'add'">确定</el-button>
        <el-button size="medium" @click="editSave" v-else>确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {mapGetters} from 'vuex';
import {objextend} from '@/utils/index';

import {savewxcompany, getwxcompanydetail, editwxcompany} from '@/api/mall/customer';
export default {
  name: 'Editcustomer',
  data() {
    return {
      labelPosition: 'left',
      forms: {
        name: '',
        corp_id: ''
      },
      userRules: {
        name: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入企业名称'
          }
        ],

        corp_id: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入企业id'
          }
        ]
      }
    };
  },
  props: {
    user_type: {
      type: String,
      default: 'add'
    },
    uid: {
      type: Number,
      default: -1
    }
  },
  computed: {
    ...mapGetters(['allroleslist'])
  },
  watch: {
    uid() {
      this.forms = {
        corp_id: '',
        name: ''
      };
      this.getlist();
    }
  },
  methods: {
    getlist() {
      if (this.uid == -1) {
        return false;
      }
      var data = {
        id: this.uid
      };
      getwxcompanydetail(data).then(res => {
        var jsondata = res.msg;
        // jsondata.status = Number(jsondata.status == '正常')
        this.forms = objextend(this.forms, jsondata);
        this.forms.id = jsondata.id;
        this.forms.update_time = jsondata.update_time;
      });
    },
    addSave() {
      var _this = this;
      this.$refs.userForm.validate(valid => {
        if (valid) {
          var data = _this.forms;
          savewxcompany(data).then(res => {
            _this.close();
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    editSave() {
      var _this = this;
      this.$refs.userForm.validate(valid => {
        if (valid) {
          var data = _this.forms;
          
          editwxcompany(data).then(res => {
            _this.close();
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    close() {
      this.forms = {
        username: '',
        name: '',
        mobile: '',
        password: '',
        role_id: '',
        status: 1
      };
      // this.uid=''
      this.$emit('on-close');
    }
  },
  mounted() {
    if (this.uid) {
      this.getlist();
    }
  }
};
</script>

<style scoped>
.el-input {
  width: 270px;
}

.box {
  width: 100%;
  /*padding: 60px;
    height: calc(100vh - 77px);
    overflow: scroll;*/
}
</style>
